package com.lute.utils;

import java.util.ArrayList;
import java.util.List;

import com.lute.model.Client;
import com.lute.model.ClientProfile;
import com.lute.model.Employee;
import com.lute.model.EmployeeProfile;
import com.lute.model.MailTemplate;

public class CreateMailForm {
	
	public static void prepareMailsInCaseEmployeeProfileChanged(Client newClient, String oldClient, EmployeeProfile employeeProfile) {
		
		int clientProfileIdNew = newClient.getClientProfil().getId_client_profile();
		List<String> clientAdminEmailsNew = ClientProfile.getClientsAdminEmail("id_client_profile", clientProfileIdNew);
		System.out.println("NewClientId"+clientProfileIdNew);
		System.out.println("NewClientSize"+clientAdminEmailsNew.size());

		int clientProfileIdOld = employeeProfile.getClient().getClientProfil().getId_client_profile();
		List<String> clientAdminEmailsOld = ClientProfile.getClientsAdminEmail("id_client_profile", clientProfileIdOld);
		System.out.println("OldClientId"+clientProfileIdOld);
		System.out.println("OldClientSize"+clientAdminEmailsOld.size());

		
		List<String> commonEmails = new ArrayList<String>();
		for(int i=0; i < clientAdminEmailsNew.size(); i++) {
			System.out.println("NewClient"+clientAdminEmailsNew.get(i));
			for(int j=0; j < clientAdminEmailsOld.size(); j++) {
				System.out.println("OldClient"+clientAdminEmailsOld.get(j));
				if(clientAdminEmailsNew.get(i).equals(clientAdminEmailsOld.get(j))) {
					commonEmails.add(clientAdminEmailsNew.get(i));
				}
			}
		}
		
		for(String common: commonEmails) {
			if(clientAdminEmailsNew.contains(common)) {
				int index = clientAdminEmailsNew.indexOf(common);
				clientAdminEmailsNew.remove(index);
			}
			if(clientAdminEmailsOld.contains(common)) {
				int index = clientAdminEmailsOld.indexOf(common);
				clientAdminEmailsOld.remove(index);
			}
		}
		
		String[] emailsNew = new String[clientAdminEmailsNew.size()];
		for(int i=0; i<clientAdminEmailsNew.size(); i++) {
			emailsNew[i] = clientAdminEmailsNew.get(i);
			System.out.println("New"+emailsNew[i]);
		}
		String[] emailsOld = new String[clientAdminEmailsOld.size()];
		for(int i=0; i<clientAdminEmailsOld.size(); i++) {
			emailsOld[i] = clientAdminEmailsOld.get(i);
			System.out.println("Old"+emailsOld[i]);
		}
		String[] emailsCommon = new String[commonEmails.size()];
		for(int i=0; i<commonEmails.size(); i++) {
			emailsCommon[i] = commonEmails.get(i);
			System.out.println("Common"+emailsCommon[i]);
		}
		
		String employeeName = employeeProfile.getEmployee().getFirst_name() +" "+
							      employeeProfile.getEmployee().getLast_name();
		if(emailsOld.length > 0) {
			/* prepare mail to the clientAdmins which were selected previously */
			String massOldMailSubject = MailTemplate.EMPLOYEE_CHANGE_CLIENTADMIN_SUBJECT.toString();
			String massOldMailBody1 = MailTemplate.EMPLOYEE_CHANGE_CLIENTADMIN_BODY1.toString();
			String massOldMailBody2 = MailTemplate.EMPLOYEE_CHANGE_CLIENTADMIN_BODY2.toString();
			String massOldMailBody = "";
			massOldMailBody1 = employeeName +" "+ massOldMailBody1 +" "+ oldClient +" to "+
							       newClient.getName()+"\n";
			massOldMailBody = massOldMailBody1 + massOldMailBody2;
			SendMail.getInstance().sendMassMail(emailsOld, massOldMailSubject, massOldMailBody);
		}
		
		if(emailsNew.length > 0) {
			/* prepare mail to the clientAdmins which are selected currently */
			String massNewMailSubject = MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_SUBJECT.toString();
			String massNewMailBody = MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_BODY1.toString();
			massNewMailBody = massNewMailBody + newClient.getName()+".\n";
			massNewMailBody = massNewMailBody + MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_BODY2.toString();
			massNewMailBody = employeeName +" "+ massNewMailBody;
			massNewMailSubject = massNewMailSubject +" "+ newClient.getName();
			SendMail.getInstance().sendMassMail(emailsNew, massNewMailSubject, massNewMailBody);
		}
		
		if(emailsCommon.length > 0) {
			String massCommonMailSubject = MailTemplate.EMPLOYEE_CHANGE_CLIENTADMIN_SUBJECT.toString();
			String massCommonMailBody = MailTemplate.EMPLOYEE_CHANGE_CLIENTADMIN_BODY1.toString();
			massCommonMailBody = employeeName +" "+ massCommonMailBody +" "+ oldClient +" to "+
								     newClient.getName()+" \n\n" +"Thanks,\nLute";
			SendMail.getInstance().sendMassMail(emailsCommon, massCommonMailSubject, massCommonMailBody);
		}
	}
	
	public static void prepareMailsInCaseNewEmployeeRegistered(Client client, Employee employee) {
		int clientProfileId = client.getClientProfil().getId_client_profile();
		List<String> clientAdminEmails = ClientProfile.getClientsAdminEmail("id_client_profile", clientProfileId);
		String[] emails = new String[clientAdminEmails.size()];
		for(int i=0; i<clientAdminEmails.size(); i++) {
			emails[i] = clientAdminEmails.get(i);
		}
		String toSubject = MailTemplate.NEW_EMPLOYEE_TO_EMPLOYEE_SUBJECT.toString();
		String toBody = MailTemplate.NEW_EMPLOYEE_TO_EMPLOYEE_BODY.toString();
		String massSubject = MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_SUBJECT.toString();
		String massBody = MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_BODY1.toString();
		massBody = massBody + client.getName()+".\n";
		massBody = massBody + MailTemplate.NEW_EMPLOYEE_TO_CLIENTADMIN_BODY2.toString();
		toSubject = toSubject + employee.getFirst_name() +" "+ employee.getLast_name();
		massSubject = massSubject +" "+ client.getName();
		massBody = employee.getFirst_name() +" "+ employee.getLast_name() +" "+ massBody;
		SendMail.getInstance().sendMassMail(emails,massSubject,massBody);
		SendMail.getInstance().sendMailTo(employee.getEmail(),toSubject,toBody);
	}
	
	public static void prepareMailInCaseEmployeeAccPeriodRejected(String email,String rejectedPerson) {
		String toSubject = MailTemplate.EMPLOYEE_ACCOUNTING_PERIOD_REJECTED_SUBJECT.toString();
		String toBody1 = MailTemplate.EMPLOYEE_ACCOUNTING_PERIOD_REJECTED_BODY1.toString();
		String toBody2 = MailTemplate.EMPLOYEE_ACCOUNTING_PERIOD_REJECTED_BODY2.toString();
		toBody1 += rejectedPerson+".\n";
		String body = toBody1+toBody2;
		SendMail.getInstance().sendMailTo(email, toSubject, body);
	}
}
